package com.qlm.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.qlm.service.IScanInfoService;
import com.qlm.tools.Common;
import com.qlm.tools.DateUtils;
import com.qlm.view.ResultMessage;
import com.qlm.view.ScanInfoView;

public class ScanInfoServiceImpl implements IScanInfoService {

	@Override
	public ResultMessage getScanInfo(String desc) {
		if(Common.isEmptyString(desc)){
			desc = desc.toLowerCase();
			if(desc.length() == 10){//判断代码长度
				String prefix = desc.substring(0, 1);
				String desc_ = desc.substring(1, desc.length());
				String idSql = "select id from t_qrcode_"+prefix+" where desc_='"+desc_+"'";
				List<Record> idList = Db.find(idSql);
				if(!Common.isNullOrEmpty(idList)){//代码是否存在
					List<ScanInfoView> list = new ArrayList<ScanInfoView>();
					for(Record r:idList){
						int id = r.getInt("id");
						String scanSql = "SELECT head_pic,nick_name,sex,scan_time,site FROM t_scan_detail s,t_user u where s.qrcode_id='"+(prefix+id)+"' and s.openid = u.openid order by scan_time";
						List<Record> infoList = Db.find(scanSql);
						if(!Common.isNullOrEmpty(infoList)){//扫码记录是否存在
							for(Record record:infoList){
								ScanInfoView s = new ScanInfoView();
								s.setHead_pic(record.getStr("head_pic")==null?"":record.getStr("head_pic"));
								s.setNick_name(record.getStr("nick_name")==null?"":record.getStr("nick_name"));
								s.setScan_time(record.getDate("scan_time")==null?"":DateUtils.dateToString(record.getDate("scan_time"),DateUtils.PATTEN_HMS));
								s.setSex(record.getStr("sex")==null?"1":record.getStr("sex"));
								s.setSite(record.getStr("site")==null?"":record.getStr("site"));
								list.add(s);
							}
						}
					}
					return new ResultMessage(1, "扫码记录查询成功", list);
				}
				return new ResultMessage(0, "代码不存在", null);
			}
			return new ResultMessage(0, "代码格式错误", null);
		}
		return new ResultMessage(0, "网络连接错误", null);
	}

}
